package org.edu.modules.classChoise.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.edu.modules.classChoise.entity.VeJwXkdj;
import org.edu.modules.classChoise.entity.VeJwXkdjkc;
import org.edu.modules.classChoise.mapper.VeJwXkdjMapper;
import org.edu.modules.classChoise.service.IVeJwXkdjService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import java.util.Map;
import java.util.ArrayList;
import java.util.List;
import java.math.BigDecimal;

/**
 * @Description: 选课登记
 * @Author:
 * @Date:   2021-05-14
 * @Version: V1.0
 */
@Service
public class VeJwXkdjServiceImpl extends ServiceImpl<VeJwXkdjMapper, VeJwXkdj> implements IVeJwXkdjService {
    @Autowired
    private VeJwXkdjMapper veJwXkdjMapper;
    /**
     * EXCEL表头
     */
    private static final String name = "学生姓名";
    private static final String xh = "学号";
    private static final String kczh = "课程组合";
    private static final String kcmc = "课程名称";
    private static final String kch = "课程号";
    private static final String zxf = "总学分";
    private static final String zxs = "总学时";
    private static final String xq = "学期";
    private static final String kcfy = "课程费用";
    private static final String spec = "专业";
    private static final String bj = "班级";
    private static final String fal = "院系";
    private static final String nj = "年级";

    @Transactional(rollbackFor = Exception.class)
    public void importDskinfo(JSONArray jsonHeadQuestionInfo) {
        VeJwXkdjServicekcImpl veJwXkdjServicekcImpl=new VeJwXkdjServicekcImpl();
        List<VeJwXkdj> VeJwXkdjDTOS = new ArrayList<>();
        List<VeJwXkdjkc> VeJwXkdjkcDTOS = new ArrayList<>();
        for (int i = 0; i < jsonHeadQuestionInfo.size(); i++) {
            JSONObject headQuestion = jsonHeadQuestionInfo.getJSONObject(i);
            String namee = headQuestion.getString(name);
            String xhe = headQuestion.getString(xh);
            String kczhe = headQuestion.getString(kczh);
            String kcmce = headQuestion.getString(kcmc);
            String kche = headQuestion.getString(kch);
            String zxfe = headQuestion.getString(zxf);
            String zxse = headQuestion.getString(zxs);
            String xqe = headQuestion.getString(xq);
            String kcfye = headQuestion.getString(kcfy);
            String spece = headQuestion.getString(spec);
            String bje = headQuestion.getString(bj);
            String fale = headQuestion.getString(fal);

            String nje = headQuestion.getString(nj);
            VeJwXkdj veJwXkdj = new VeJwXkdj();
            VeJwXkdjkc veJwXkdjkc = new VeJwXkdjkc();

            List<Map<String, Object>> list = this.selectSemidPage(xqe);
            if ((list != null) && (list.size() > 0)) {
                int xqid=(int)list.get(0).get("id");
                veJwXkdj.setSemid(xqid);
            }
            List<Map<String, Object>> fallist = this.selectFalidPage(fal);
            if ((fallist != null) && (fallist.size() > 0)) {
                int falid=(int)list.get(0).get("id");
                veJwXkdj.setFalid(falid);
            }
            List<Map<String, Object>> spelist = this.selectSpeidPage(spec);
            if ((spelist != null) && (spelist.size() > 0)) {
                int speid=(int)spelist.get(0).get("id");
                veJwXkdj.setSpecid(speid);
            }
            List<Map<String, Object>> bjlist = this.selectBjidPage(bj);
            if ((bjlist != null) && (bjlist.size() > 0)) {
                int bjid=(int)bjlist.get(0).get("id");
                veJwXkdj.setBjid(bjid);
            }
       //     String kcmc1[]=kcmc.split(",");

            // 问题内容不能为空
           /* if (StringUtils.isBlank(namee)) {
                throw new RuntimeException("存在学生姓名为空的记录");
            }
            // 问题类型不能为空
            if (StringUtils.isBlank(questiontype)) {
                throw new RuntimeException("存在问题类型为空的问题");
            }
            // 分数不能为空
            if (StringUtils.isBlank(fs)) {
                throw new RuntimeException("存在默认分数为空的问题");
            }else
            {if(!isNumeric(fs)){
                throw new RuntimeException("存在非数字的分数");
            }
            }
            // 类别不能为空
            if (StringUtils.isBlank(lb)) {
                throw new RuntimeException("存在类别为空的问题");
            }
            // 难易度不能为空
            if (StringUtils.isBlank(nyd)) {
                throw new RuntimeException("存在难易度为空的问题");
            }
            // 状态不能为空
            if (StringUtils.isBlank(state)) {
                throw new RuntimeException("存在状态为空的问题");
            }*/



         //   for(int j=0;j<kcmc1.length;j++){
        //        veJwXkdjkc.setStunum(xhe);
         //       veJwXkdjkc.setKcmc(kcmc1[j]);
         //       VeJwXkdjkcDTOS.add(veJwXkdjkc);

                //veJwXkdjkc.setKch(fale);
            //}
            veJwXkdj.setStuname(namee);
            veJwXkdj.setStunum(xhe);
            veJwXkdj.setKczh(kczhe);
            BigDecimal bdzxfe=new BigDecimal(zxfe);
            veJwXkdj.setKcmc(kcmce);
            veJwXkdj.setZxf(bdzxfe);
            veJwXkdj.setZxs(Integer.parseInt(zxse));
            veJwXkdj.setSemname(xqe);
            BigDecimal bdkcfye=new BigDecimal(kcfye);
            veJwXkdj.setKcfy(bdkcfye);
            veJwXkdj.setSpec(spece);
            veJwXkdj.setBj(bje);
            veJwXkdj.setFal(fale);

            veJwXkdj.setFal(nje);

            VeJwXkdjDTOS.add(veJwXkdj);
        }

        for (VeJwXkdj veJwXkdjDTO : VeJwXkdjDTOS) {
            VeJwXkdj veJwXkdj = new VeJwXkdj();
            BeanUtil.copyProperties(veJwXkdjDTO,veJwXkdj);
            this.save(veJwXkdj);

        }
       /* for (VeJwXkdjkc veJwXkdjkcDTO : VeJwXkdjkcDTOS) {
            VeJwXkdjkc veJwXkdjkc = new VeJwXkdjkc();
            BeanUtil.copyProperties(veJwXkdjkcDTO, veJwXkdjkc);
            veJwXkdjServicekcImpl.save(veJwXkdjkc);
        }*/
    }


    @Override
    public List<Map<String, Object>> selectSemidPage(String xq) {
        return veJwXkdjMapper.selectSemidPage(xq);

    }
    @Override
    public List<Map<String, Object>> selectFalidPage(String fal) {
        return veJwXkdjMapper.selectFalidPage(fal);

    }
    @Override
    public List<Map<String, Object>> selectBjidPage(String bj) {
        return veJwXkdjMapper.selectBjidPage(bj);

    }
    @Override
    public List<Map<String, Object>> selectSpeidPage(String spe) {
        return veJwXkdjMapper.selectSpeidPage(spe);

    }



}
